﻿using System.Text.RegularExpressions;
using AllInOnePluginBL.Helpers;
using PluginDB;

namespace AllInOnePluginBL.SiteInfoScrappers
{
    

    /// <summary>
    /// Summary description for TorecNetScraper
    /// </summary>
    public class TorecNetScraper
    {
        const string BASE_URL = "http://www.torec.net/ssearch.asp";
        
        public static TVDBInformationItem GetVideoInformation(string searchName)
        {
            var tvdbInformationItem = new TVDBInformationItem { Name = searchName };

            if (string.IsNullOrEmpty(searchName))
            {
                tvdbInformationItem.IsValid = false;
                return tvdbInformationItem;
            }


            var myPost = new GenericWebRequest();
            myPost.Add("search", searchName);
            var response = myPost.GetResponse(BASE_URL, GenericWebRequest.PostMethod.POST);

            const string INFO_REGEX = "class=\"newd_table_titleRight_BG\".*?<a href=\"(.+?)\">(.+?)</a>.*?<param name=\"flashvars\" value=\"image=(.+?)\".*?<span style=\"font-weight:400;\">(.+?)</span>";
            var matches = Regex.Matches(response, INFO_REGEX, RegexOptions.Singleline);
            if (matches.Count == 0) return tvdbInformationItem;

            tvdbInformationItem.FanartUrl = "http://www.torec.net" + matches[0].Groups[3].Value;
            tvdbInformationItem.Name = matches[0].Groups[2].Value;
            tvdbInformationItem.Plot = matches[0].Groups[4].Value;
            tvdbInformationItem.IsValid = true;

            return tvdbInformationItem;
        }
    }
}